/**
 ******************************************************************************
 * @file    main.c
 * @author  MCU Application Team
 * @brief   Main program body
 * @date
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) AirM2M.
 * All rights reserved.</center></h2>
 *
 * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private define ------------------------------------------------------------*/
#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
static void APP_SystemClockConfig(void);
/* Private variables ---------------------------------------------------------*/
// UART_HandleTypeDef UartHandle;
uint8_t rx_flag = 0;
uint8_t aTxBuffer[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
uint8_t aRxBuffer[22] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
uint8_t init_text[] = "Please input count sec number";
/* Private user code ---------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/

void key_solve_func(void)
{
  uint8_t ucKeyCode = 0;
  /* 按键滤波和检测由后台systick中断服务程序实现，我们只需要调用bsp_GetKey读取键值即可。 */
  ucKeyCode = bsp_GetKey(); /* 读取键值, 无键按下时返回 KEY_NONE = 0 */
  if (ucKeyCode != KEY_NONE)
  {
    switch (ucKeyCode)
    {
    case MINUS_KEY_DOWN: /* 减键按下 */
      printf("K1键按下\r\n");
      break;
    case ADD_KEY_DOWN: /* 加键按下 */
      printf("K2键按下\r\n");
      break;
    case START_KEY_DOWN: /* 开始键按下 */
      printf("K3键按下\r\n");
      break;
    case SET_KEY_DOWN: /* 设置键按下 */
      printf("K4键按下\r\n");
      break;
    case FUNC_KEY_DOWN: /* 功能键按下 */
      printf("K5键按下\r\n");
      break;
    case POW_KEY_DOWN: /* 省电键按下 */
      printf("K6键按下\r\n");
      break;
    default:
      break;
    }
  }
}
/**
 * @brief  应用程序入口函数.
 * @retval int
 */
int main(void)
{
  int32_t aADCxConvertedData = 0;
  int16_t i = 0;
  int16_t aTEMPERATURE = 0;

  /* systick初始化 */
  HAL_Init();
  /* 系统时钟配置 */
  APP_SystemClockConfig();
  Bsp_Uart_Init();
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  bsp_InitKey();
  /* 配置ADC */
  APP_AdcConfig();
  TIM_Init();
  user_spi_init();
	LCD_GPIO_Init();
  if (HAL_UART_Receive_IT(&DebugUartHandle, (uint8_t *)aRxBuffer, 8) != HAL_OK)
  {
    Error_Handler();
  }
  while (1)
  {
    key_solve_func(); // 按键处理函数
    BSP_LED_Toggle(LED1);
    HAL_Delay(500);
    BSP_LED_Toggle(LED2);
    HAL_Delay(500);
  }

  // BSP_LED_Toggle(LED3);
  // HAL_Delay(500);
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart->Instance == DEBUG_USART)
  {
    if ((aRxBuffer[0] == 0xaa) && (aRxBuffer[1] == 0xbb) && (aRxBuffer[7] == 0xbb))
    {
      if (aRxBuffer[2] == 0x01) // 倒计时
      {
        printf("Receive data complete\r\n");
      }
      else
      {
        printf("Please input count sec number\r\n");
      }
    }
    if (HAL_UART_Receive_IT(&DebugUartHandle, (uint8_t *)aRxBuffer, 8) != HAL_OK)
    {
      Error_Handler();
    }
  }
  // rx_flag = 1;
  // if (HAL_UART_Receive_IT(&DebugUartHandle, (uint8_t *)aRxBuffer, 22) != HAL_OK)
  // {
  //   Error_Handler();
  // }
}
/**
 * @brief  系统时钟配置函数
 * @param  无
 * @retval 无
 */
static void APP_SystemClockConfig(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /* 振荡器配置 */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; /* 选择振荡器HSE,HSI,LSI,LSE */
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;                                                                                              /* 开启HSI */
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;                                                                                              /* HSI 1分频 */
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_16MHz;                                                                     /* 配置HSI时钟16MHz */
  RCC_OscInitStruct.HSEState = RCC_HSE_OFF;                                                                                             /* 关闭HSE */
  /*RCC_OscInitStruct.HSEFreq = RCC_HSE_16_32MHz;*/
  RCC_OscInitStruct.LSIState = RCC_LSI_OFF; /* 关闭LSI */
  RCC_OscInitStruct.LSEState = RCC_LSE_OFF; /* 关闭LSE */
  /*RCC_OscInitStruct.LSEDriver = RCC_LSEDRIVE_MEDIUM;*/
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;         /* 开启PLL */
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; /* 选择时钟源为HSI */
  /* 配置振荡器 */
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /* 时钟源配置 */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1; /* 选择配置时钟 HCLK,SYSCLK,PCLK1 */
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;                                      /* 选择PLL作为系统时钟 */
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;                                             /* AHB时钟 1分频 */
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;                                              /* APB时钟 1分频 */
  /* 配置时钟源 */
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}
/**
 * @brief  错误执行函数
 * @param  无
 * @retval 无
 */
void Error_Handler(void)
{
  uint32_t i = 0;
  printf("Error_Handler \r\n");
  /* 无限循环 */
  while (1)
  {
    i++;
  }
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  输出产生断言错误的源文件名及行号
 * @param  file：源文件名指针
 * @param  line：发生断言错误的行号
 * @retval 无
 */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* 用户可以根据需要添加自己的打印信息,
     例如: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* 无限循环 */
  while (1)
  {
  }
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT AirM2M *****END OF FILE******************/
